package com.ssyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssyx.acl.service.AdminService;
import com.ssyx.acl.service.RoleService;
import com.ssyx.common.result.Result;
import com.ssyx.model.acl.Admin;
import com.ssyx.vo.acl.AdminQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:AdminController
 * Description:
 *
 * @Author kay
 * @Create 2024/4/23 22:11
 * @Version 1.0
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
//@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Operation(summary = "获取管理用户分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        AdminQueryVo adminQueryVo){
        Page<Admin> pageParam = new Page<>(page, limit);
        //调用service方法实现条件分页查询,返回分页对象
        IPage<Admin> pageModel = adminService.getAdminPage(pageParam,adminQueryVo);
        return Result.ok(pageModel);
    }

    @Operation(summary = "获取管理用户")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    @Operation(summary = "新增管理用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin){
        String password = admin.getPassword();
        String MD5_password = DigestUtils.md5DigestAsHex(password.getBytes());
        admin.setPassword(MD5_password);
        adminService.save(admin);
        return Result.ok(null);
    }

    @Operation(summary = "修改管理用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin){
        adminService.updateById(admin);
        return Result.ok(null);
    }

    @Operation(summary = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        adminService.removeById(id);
        return Result.ok(null);
    }

    @Operation(summary = "批量删除管理用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        adminService.removeBatchByIds(idList);
        return Result.ok(null);
    }

    @Operation(summary = "根据用户获取角色数据")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        //返回map集合包含两部分数据:所有角色,为用户已分配过的角色
        Map<String,Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    @Operation(summary = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam Long[] roleIds){
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return Result.ok(null);
    }


}
